package com.anan.service.impl;

import com.anan.constants.Constants;
import com.anan.exception.BusinessException;
import com.anan.mapper.ImgResourceMapper;
import com.anan.mapper.OpusMapper;
import com.anan.mapper.ProductMapper;
import com.anan.mapper.StudentMapper;
import com.anan.pojo.bo.ImgInfosBO;
import com.anan.pojo.bo.OpusBO;
import com.anan.pojo.domain.ImgResourceDO;
import com.anan.pojo.domain.OpusDO;
import com.anan.pojo.domain.ProductDO;
import com.anan.pojo.domain.StudentDO;
import com.anan.pojo.dto.ImgDTO;
import com.anan.pojo.dto.ProductDTO;
import com.anan.pojo.dto.StudentDTO;
import com.anan.pojo.vo.StudentAndOpusVO;
import com.anan.pojo.vo.StudentAndProdInfoVO;
import com.anan.pojo.vo.StudentVO;
import com.anan.service.ImgResourceService;
import com.anan.service.StudentService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;


@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;
    @Resource
    private ImgResourceMapper imgResourceMapper;
    @Resource
    private ProductMapper productMapper;
    @Resource
    private ImgResourceService imgResourceService;
    @Resource
    private OpusMapper opusMapper;

    @Override
    public int insertStudent(StudentDTO studentDTO) {
        int num = 0;
        if(studentDTO != null){
            String username = studentDTO.getUsername();
            StudentDO student = studentMapper.getStudentByUsername(username);
            if(student == null) {
                num = studentMapper.insertStudent(studentDTO);
            }else {
                throw new BusinessException(Constants.RET_CODE.FAILED, "用户已存在！");
            }
        }
        return num;
    }

    @Override
    public StudentVO login(StudentDTO studentDTO) {
        if (studentDTO != null){
            StudentVO studentVO = new StudentVO();
            StudentDO studentDO = studentMapper.getStudentByUsernameAndPassword(studentDTO.getUsername(), studentDTO.getPassword());
            if (null != studentDO) {
                BeanUtils.copyProperties(studentDO, studentVO);
                ImgResourceDO headResourceDO = imgResourceMapper.getHeadUrlByStudentId(Constants.TYPE.STUDENT, studentDO.getStudentId());
                if (null != headResourceDO) studentVO.setHeadUrl(headResourceDO.getImgUrl());
                return studentVO;
            }
        }
        return null;
    }

    @Override
    public boolean updateStudentInfo(StudentDTO studentDTO) {
        if (null != studentDTO) {
            boolean flag = false;
            StudentDO studentDO = studentMapper.getStudentByUsername(studentDTO.getUsername());
            if (null != studentDO) {
                //更新学生信息
                studentMapper.updateStudent(studentDTO);

                List<ImgResourceDO> imgResources = imgResourceMapper.getUrlByIdAndType(Constants.TYPE.STUDENT, studentDO.getStudentId());
                if (!CollectionUtils.isEmpty(imgResources)) {
                    if (imgResources.size() > 1) {
                        throw new BusinessException("查询用户头像出现多个！");
                    } else {
                        ImgResourceDO imgResourceDO = imgResources.get(0);
                        imgResourceDO.setImgUrl(studentDTO.getHeadUrl());
                        flag = imgResourceMapper.updateImg(studentDTO.getHeadUrl(), imgResourceDO.getImgType(), imgResourceDO.getRelateId());
                    }
                } else {
                    ImgDTO imgDTO = new ImgDTO();
                    List<ImgInfosBO> imgInfosBOS = new ArrayList<>();
                    ImgInfosBO imgInfosBO = new ImgInfosBO();
                    imgInfosBO.setImgType(Constants.TYPE.STUDENT);
                    imgInfosBO.setRelateId(studentDO.getStudentId());
                    imgInfosBOS.add(imgInfosBO);
                    imgDTO.setImgInfosBOs(imgInfosBOS);

                    imgResourceMapper.addImgs(imgDTO);
                }

                return flag;
            }
        }
        return false;
    }

    @Override
    public StudentAndProdInfoVO getUserStudentAndProdByIdById(Integer studentId) {
        if(null != studentId) {
            StudentAndProdInfoVO studentAndProdInfoVO = new StudentAndProdInfoVO();
            StudentDO studentDO = studentMapper.getStudentById(studentId);
            BeanUtils.copyProperties(studentDO, studentAndProdInfoVO);
            if (null != studentDO) {
                //查询头像信息
                ImgResourceDO imgResourceDO = imgResourceMapper.getHeadUrlByStudentId(Constants.TYPE.STUDENT, studentId);
                studentAndProdInfoVO.setHeadUrl(null != imgResourceDO ? imgResourceDO.getImgUrl() : null);

                //查询商品信息
                List<ProductDO> products =  productMapper.getProductsByIdAndStatus(studentId, Constants.PRODUCT_STATE.SHELVES_ON, Constants.ADMIN_STATUS.PASS);
                List<ProductDTO> productDTOS = new ArrayList<>();
                studentAndProdInfoVO.setProducts(productDTOS);
                products.forEach(productDO -> {
                    ProductDTO productDTO = new ProductDTO();
                    BeanUtils.copyProperties(productDO, productDTO);
                    productDTOS.add(productDTO);

                    //查询商品图片信息
                    List<ImgInfosBO> imgInfosBOs = new ArrayList<>();
                    productDTO.setImgInfosBOs(imgInfosBOs);
                    List<ImgResourceDO> imgResources = imgResourceMapper.getUrlByIdAndType(Constants.TYPE.PRODUCT, productDO.getProductId());
                    imgResources.forEach(bean -> {
                        ImgInfosBO imgInfosBO = new ImgInfosBO();
                        BeanUtils.copyProperties(bean, imgInfosBO);
                        imgInfosBOs.add(imgInfosBO);

                    });
                });
                return studentAndProdInfoVO;
            }

        }
        return null;
    }

    @Override
    public StudentAndOpusVO getStudentAndOpusById(Integer studentId) {
        if (null != studentId) {
            StudentAndOpusVO studentAndOpusVO = new StudentAndOpusVO();
            //获取用户信息
            StudentDO studentDO = studentMapper.getStudentById(studentId);
            BeanUtils.copyProperties(studentDO, studentAndOpusVO);

            if (null == studentDO) throw new BusinessException("该用户不存在！");

            //查询头像信息
            ImgResourceDO imgResourceDO = imgResourceMapper.getHeadUrlByStudentId(Constants.TYPE.STUDENT, studentId);
            studentAndOpusVO.setHeadUrl(null != imgResourceDO ? imgResourceDO.getImgUrl() : null);

            //获取用户的作品信息
            List<OpusDO> opusList = opusMapper.getOpusByIdAndStatus(studentId, Constants.PRODUCT_STATE.SHELVES_ON, Constants.ADMIN_STATUS.PASS);
            List<OpusBO> products = new ArrayList<>();
            studentAndOpusVO.setProducts(products);
            opusList.forEach(opusDO -> {
                OpusBO opusBO = new OpusBO();
                BeanUtils.copyProperties(opusDO, opusBO);
                SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd :hh:mm:ss");
                String createDate = simpleDateFormat.format(opusDO.getCreateDate());
                opusBO.setCreateDate(createDate);
                products.add(opusBO);

                //获取用户作品的图片信息
                List<ImgInfosBO> imgInfosBOs = new ArrayList<>();
                opusBO.setImgInfosBOs(imgInfosBOs);
                List<ImgResourceDO> imgResources = imgResourceMapper.getUrlByIdAndType(Constants.TYPE.OPUS, opusDO.getOpusId());
                imgResources.forEach(bean -> {
                    ImgInfosBO imgInfosBO = new ImgInfosBO();
                    BeanUtils.copyProperties(bean, imgInfosBO);
                    imgInfosBOs.add(imgInfosBO);

                });
            });

            return studentAndOpusVO;
        }
        return null;
    }
}
